<template>
  <base-breadcrumb :title="$route.meta.title">
    <!-- 技术整改通知单 -->
    <div
      id="base-breadcrumb"
      style="position: relative; z-index: 1"
    >
      <!-- 搜索 -->
      <searchData
        :search-data="searchArr"
        @search="search"
      />
      <!-- 表格 -->
      <a-card :bordered="false">
        <!--表格-->
        <list-table
          ref="table"
          row-key="id"
          :columns="columns"
          :isEportIndex="true"
          @select-click="selectClick"
          :request="loadData"
          :startIndex="0"
          :select-config="{ use: true, type: 'checkbox', rowKey: 'id' }"
          :export-config="{ use: true, select: '技术整改通知单', all: '技术整改通知单' }"
          :export-config-text="{select:'导出选中列表',all:'导出全部列表'}"
          :exportPermission="{select: 'qm_055_aexp_03',all: 'qm_055_sexp_04'}"
          :isShowCountBar="false"
          :pageKeys="['pageNum', 'pageSize', 'total']"
        >
          <template slot="prefix">
            <a-button
                    v-if="hideBuquBtn()"
              v-btn-permission="'qm_055_addre_01'"
              type="primary"
              icon="plus"
              @click="handleManage(0)"
            >新增整改单</a-button>
          </template>
          <a-button
            v-btn-permission="'qm_055_aexpre_05'"
            type="primary"
            slot="suffix"
            :loading="exportLoading"
            slot-scope="scoped"
            icon="cloud-download"
            :disabled="scoped.selectedRowKeys && scoped.selectedRowKeys.length === 0"
            @click="exportWord(scoped.selectedRowKeys)"
          >导出选中整改单</a-button>
          <span
            slot="sort"
            slot-scope="text, record, index"
          >{{ index + 1 }}</span>
          <more-button
            slot="actions"
            slot-scope="text, record"
          >
            <a
              v-btn-permission="'qm_055_loo_06'"
              @click="handleManage(2, record.id,record.status, record)"
            >查看</a>
            <a
              v-if="record.isOperation&&hideBuquBtn()"
              @click="handleManage(3, record.id)"
            >验证</a>
            <a
              v-btn-permission="'qm_055_edi_07'"
              v-if="record.status == 0&&hideBuquBtn()"
              @click="handleManage(1, record.id, 1, record)"
            >编辑</a>
            <a
              v-btn-permission="'qm_055_del_08'"
              v-if="record.status == 0&&hideBuquBtn()"
              class="del_btn"
              @click="handleDelete(record.id)"
            >删除</a>
          </more-button>
          <!-- 汇总 -->
          <template slot="bottom">
            <AuditSummary
              slot="bottom"
              ref="AuditSummary"
              :countList="countList"
              :selectLength="selectLength"
              :form="queryParam"
            />
          </template>
          <!-- 状态格式化 -->
          <StatusDisplay
            slot="statusText"
            slot-scope="text, record"
            :status-code="record.status"
            :statusMap="statusMap"
          />
        </list-table>
      </a-card>
    </div>
  </base-breadcrumb>
</template>

<script>
import OrgTreeSelect from '@/components/OrgTreeSelect'
import { rectificationNotice,rectificationNoticeTwo,rectificationNoticeThree} from '@/api/quality/quality' // api
import  { queryByPageTechnicalProblemNotice, technicalProblemNoticeBatchPrint, deleteTechnicalProblemNotice } from "@/api/technology"
import SelectProjectStatus from '@/views/quality/components/SelectProjectStatus' // 项目状态
import AuditSummary from '../components/AuditSummary' // 汇总
import StatusDisplay from '@/components/StatusDisplay'
import MoreButton from '@/components/MoreButton' // 多按钮容器
import BasePage from '@/views/basePage'
import moment from "moment"
const searchArr = [
  // 搜索框
  {
    name: '受检单位',
    type: OrgTreeSelect,
    valueKey: 'searchQuery',
    attrs: {isAllowNnect: true}
  },
  {
    name: '接收单位',
    type: 'BaseInput',
    valueKey: 'receivingUnit'
  },
  {
    name: '下发人',
    type: 'BaseInput',
    valueKey: 'issueEmployeeName'
  },
  {
    name: '下发时间',
    value: 'checkTime',
    type: 'dateRange',
    attr: {
      rangeFormat: ['createTimeStart', 'createTimeEnd']
    }
  },
  {
    name: '状态',
    value: 'status',
    type: 'select',
    data: [
      {
        label: '待提交',
        value: 0
      },
      {
        label: '待处理',
        value: 1
      },
      {
        label: '整改中',
        value: 2
      },
      {
        label: '合格',
        value: 3
      },
      {
        label: '不合格',
        value: 4
      }
    ]
  }
]
const columns = [
  {
    title: '序号',
    dataIndex: 'index',
    width: 80,
    scopedSlots: { customRender: 'sort' }
  },
  {
    title: '受检单位',
    ellipsis: true,
    dataIndex: 'inspectedUnitName'
  },
  {
    title: '接收单位',
    ellipsis: true,
    dataIndex: 'receivingUnit'
  },
  {
    title: '下发人',
    ellipsis: true,
    dataIndex: 'creatorName'
  },
  {
    title: '下发时间',
    ellipsis: true,
    dataIndex: 'createTime'
  },
  {
    title: '整改期限',
    ellipsis: true,
    dataIndex: 'rectifyTerm'
  },
  {
    title: '问题数量',
    ellipsis: true,
    dataIndex: 'problemCount'
  },
  {
    title: '整改合格数量',
    dataIndex: 'rectifyQualifiedCount'
  },
  {
    title: '状态',
    dataIndex: 'statusText',
    scopedSlots: { customRender: 'statusText' }
  },
  {
    title: '操作',
    dataIndex: 'actions',
    width: '150px',
    align: 'center',
    scopedSlots: { customRender: 'actions' }
  }
]
const statusMap = {
  0: {
    label: '待提交',
    color: 'gray'
  },
  1: {
    label: '待处理',
    color: 'orange'
  },
  2: {
    label: '整改中',
    color: 'orange'
  },
  3: {
    label: '合格',
    color: '#5be65dd4'
  },
  4: {
    label: '不合格',
    color: 'red'
  },
  5: {
    label: '已生成',
    color: '#5be65dd4'
  }
}
export default {
  name: 'a' + Date.now(),
  extends: new BasePage(),
  components: {
    AuditSummary,
    StatusDisplay,
    MoreButton
  },
  data() {
    this.columns = columns
    this.searchArr = searchArr
    this.statusMap = statusMap
    return {
      exportLoading: false, // 导出加载控件
      visible: false,
      // 表格加载
      queryParam: {}, // 查询参数
      selectLength:0,
      newTotal:0,
      countList: [
        {
          title: '已选中：',
          value: 'selectLength'
        },
        {
          title: '技术整改数',
          value: 'rectificationCount'
        },
        {
          title: '待整改',
          value: 'waitRectificationCount'
        },
        {
          title: '整改中',
          value: 'rectificationConductCount'
        },
        {
          title: '待复查',
          value: 'waitReviewCount'
        },
        {
          title: '复查通过',
          value: 'reviewPassCount'
        },
        {
          title: '不合格不整改',
          value: 'unqualifiedNoRectificationCount'
        },
        {
          title: '整改超时',
          value: 'overtimeRectificationCount'
        }
        // {
        //   title: '整改完成率',
        //   value: 'rectificationRate'
        // }
      ], // 汇总列表
      api: rectificationNoticeThree,
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, {param: this.queryParam})
        return queryByPageTechnicalProblemNotice(requestParameters).then((res) => {
          res.data.list.forEach((item,index)=>{
            item.statusText = this.statusMap[item.status]?.label
            item.createTime = moment(item.createTime).format('YYYY-MM-DD')
          })
          this.newTotal = res.totalSize;
          return res
        })
      }
    }
  },
  methods: {
      selectClick(a,b){
      this.selectLength = a.rows.length;
    },
    /**
     * @description: 搜索
     * @param {*} value：表单对象
     */
    search(value) {
      this.queryParam = value
      this.$refs.table.refresh(true)
      this.$nextTick(()=>{
        this.$refs.AuditSummary?.getCount()
      })
    },
    /**
     * @description: 跳转操作页
     * @param {*} type：操作类型： 0 新增；1 编辑；2 查看；3 验证；4 生成整改单
     * @param {*} id：业务id
     */
    handleManage(type, id,status, record={}) {
      this.$router.push({
        name: 'rectificationNoticeEditS',
        query: {
          id, // 业务id
          editModel: type, // 操作类型
          status,
          isOperation: type == 0 ? null: (record.isOperation ? 1 : 0)
        }
      })
    },
    /**
     * @description: 删除记录
     * @param {*} id：业务id
     * @return {*}
     */
    handleDelete(id) {
      let that = this
      that.$confirm({
        title: '温馨提示',
        content: '确认删除？',
        okText: '确认',
        cancelText: '关闭',
        onOk() {
          return deleteTechnicalProblemNotice({ id }).then((res) => {
            that.$message.success('删除成功！')
            that.$refs.table.loadData()
          })
        }
      })
    },
    /**
     * @description: 导出word
     * @param {*} datas：选中项
     */
    exportWord(datas) {
      this.exportLoading = true
      technicalProblemNoticeBatchPrint({ ids: datas })
        .then((res) => {
            // 格式化名称，防止重名被覆盖
            res.data.forEach((i, idx) => {
              i.name = Number(idx) + 1 + '.' + i.name
            })
            this.exportPointInfo(res.data, '技术整改通知单')
        })
        .finally(() => {
          this.exportLoading = false
        })
    }
  }
}
</script>


